package whu.cn.whushare.ui.main.known;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.android.volley.VolleyError;
import com.androidplus.util.ViewMapping;
import com.androidplus.util.ViewMappingUtil;

import whu.cn.whushare.R;
import whu.cn.whushare.net.DownloadParams;
import whu.cn.whushare.net.Downloader;
import whu.cn.whushare.ui.common.BaseActivity;
import whu.cn.whushare.util.SetOnClickUtil;

/**
 * 完成“提问”页面的操作
 */
@ViewMapping(id = R.layout.activity_ask_question)               //获取提问（activity_ask_question）布局文件
public class PublishQuestionActivity extends BaseActivity {
	@ViewMapping(id = R.id.button_one)                          //获取“问题”按钮
	private Button mQuestionBtn;
	@ViewMapping(id = R.id.button_two)                          //获取“描述”按钮
	private Button mDescBtn;
	@ViewMapping(id = R.id.button_three)                        //获取“话题”按钮
	private Button mTopicBtn;
	@ViewMapping(id = R.id.question_edit)                       //获取“问题”编辑框
	private EditText mQEdit;
    @ViewMapping(id = R.id.desc_edit)                           //获取“问题描述”编辑框
    private EditText mDEdit;
    @ViewMapping(id = R.id.topic_edit)                          //获取“话题”编辑框
    private EditText mTEdit;
	@ViewMapping(id = R.id.checkBox)                            //获取“匿名”多选框
	private CheckBox mCheckBox;
    private AlertDialog.Builder dialog;                         //声明一个对话框
   private CharSequence[] items = {"薪酬福利","员工关系", "绩效管理","职称评审","档案管理","招聘选拔","企业文化",
            "培训开发",  "政策法规", "政策法规","人力资源战略与规划", "人力资源信息系统", "人力资源组织设计与工作分析"};

    //获取跳转到当前activity的intent信息
	public static Intent getStartIntent(Context context) {
		return new Intent(context, PublishQuestionActivity.class);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
	}

    //初始化页面，主要对TopBar进行设置
	private void initView() {
        View root = ViewMappingUtil.mapView(this, this);
        getTopBar(root);
        SetOnClickUtil.registerOnClick(this);

        mTopBar.rightText.setVisibility(View.VISIBLE);
        mTopBar.rightText.setText(R.string.publish);
        mTopBar.rightText.setOnClickListener(this);
    }

    //发布问题，创建一个“发布问题”的网络请求
	private void publishQuestion() {
		mTopBar.rightText.setProgress(1);

        String mEditStr=mQEdit.getText().toString();        //获取“问题”
        String mEditStr1=mDEdit.getText().toString();       //获取“问题描述”
        String mEditStr2=mTEdit.getText().toString();       //获取“话题”

		Downloader.getInstance(this).publishQuestion(mEditStr, mEditStr1, mEditStr2, this, null);

	}

	@Override
    //“发布问题”的网络请求成功响应时调用
	public <T> void onSuccess(DownloadParams.PARSING_TYPE type, T response, Object obj) {
		super.onSuccess(type, response, obj);
		mTopBar.rightText.setProgress(100);
		mTopBar.rightText.setText(R.string.publish);
		if (code == 1) {
			toast(msg);
			finish();
		}
	}

	@Override
    //“发布问题”的网络请求响应失败时调用
	public void onFail(DownloadParams.PARSING_TYPE type, VolleyError error) {
		super.onFail(type, error);
		mTopBar.rightText.setProgress(100);
		mTopBar.rightText.setText(R.string.publish);
		toast(msg);
	}

	@Override
    /**
     * 方法名：onClick(View v)
     * 功 能：根据发生点击事件的控件不同处理不同的点击事件
     *        控件为“问题”按钮时，设置布局文件为问题视图
     *        控件为“问题描述”按钮时，设置页面布局为问题描述视图
     *        控件为“话题”按钮时，设置页面布局为话题视图，并显示话题选择对话框
     *        控件为“发布”按钮时，创建一个“发布问题”的网络请求
     * 参 数：@param v - 发生点击事件的控件
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.button_one:
            mQEdit.setVisibility(View.VISIBLE);
            mDEdit.setVisibility(View.GONE);
            mTEdit.setVisibility(View.GONE);
            break;

		case R.id.button_two:
            mQEdit.setVisibility(View.GONE);
            mDEdit.setVisibility(View.VISIBLE);
            mTEdit.setVisibility(View.GONE);
            break;

		case R.id.button_three:
            showVoteDialog();
            mQEdit.setVisibility(View.GONE);
            mDEdit.setVisibility(View.GONE);
            mTEdit.setVisibility(View.VISIBLE);
			break;

		case R.id.top_right_txt:
			publishQuestion();
			break;
		default:
			break;
		}
	}

    //显示话题选择对话框
    private void showVoteDialog() {
        if (dialog == null) {
            dialog = new AlertDialog.Builder(this);
            dialog.setAdapter(new ArrayAdapter(this,
                    R.layout.topic_items,items), new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int item) {
                    mTEdit.setText(mTEdit.getText().toString()+items[item]+",");
                }
                });
        }
        if (!isFinishing()) {
            dialog.show();
        }
    }
}
